import {ScrollText} from "../ScrollText/ScrollText.js";

function scrollButton(cup) {
    this.coef = 1;
    // 回调函数
    this.clickCallBack = null;
    // 获得容器
    this.cup = cup;
    if(!this.cup) return false;

    // 子组件
    this.scrollText = null

    // 在容器内创建结构
    this.__initStruct();

    // 获得组件
    this.ScrollButton = this.cup.querySelector(".ScrollButton");
}

scrollButton.prototype.__initStruct = function() {
    let content = this.cup.innerText;

    let templet = `
        <div class="ScrollButton">
            <button>
                <div class="scrollButtonLabel">
                </div>
            </button>
        </div>
    `;
    this.cup.innerHTML = templet;
    this.scrollText = ScrollText(this.cup.querySelector(".scrollButtonLabel"));
    this.scrollText.setText(content);
}

// 设置回调函数
scrollButton.prototype.setClickCallBack = function(callBack) {
    if(typeof callBack !== "function")
        return false;
    this.clickCallBack = callBack;
}

// 执行回调函数
scrollButton.prototype.__runClickCallBack = function() {
    if(typeof this.clickCallBack !== "function")
        return false;
    this.clickCallBack();
}

// 启动监听
scrollButton.prototype.addClickRespon = function() {
    this.ScrollButton.addEventListener("click", () => {
        this.__runClickCallBack();
    })
}

// 返回框架
scrollButton.prototype.getScrollButton = function() {
    return this.ScrollButton;
}

// 返回容器
scrollButton.prototype.getCup = function() {
    return this.cup;
}

// 大小设置 系数
scrollButton.prototype.setSize = function(coef) {
    if(!this.scrollText)
        return false;
    this.coef = coef || this.coef;

    this.scrollText.setRate(this.coef);
    this.ScrollButton.style.setProperty("--controlLen", String(window.innerWidth * this.coef));
}

// 更改标签内容
scrollButton.prototype.changeContent = function(content) {
    if(typeof content !== "string" || !this.scrollText)
        return false;

    this.scrollText.setText(content);
}

function createScrollButton(selector) {
    return new scrollButton(selector || null);
}

export var ScrollButton = createScrollButton;